<?php defined('SYSPATH') or die('No direct script access.');

class Controller_BoFeedback extends Controller_BoBase {
	public $auth_required = TRUE;

	public function action_index()
	{
		$view = View::factory("pages/bo/home");
		$this->setTab($view);
		$this->baseInfo($view);
		$this->request->response = $view;
	}

	public function action_show(){
		$view = View::factory("pages/bo/feedback/feedback_page");
		$this->setTab($view);
		$this->baseInfo($view);
		exit($view->render());
	}
	
	public function action_process(){
		$feedback = ORM::factory("feedback",$_REQUEST["fbid"]);
		$feedback->process = "Y";
		$feedback->save();
		exit("已处理");
	}

	public function action_search(){
		$view = View::factory("pages/bo/feedback/feedback_page");

		$feedback = ORM::factory("feedback")->where("status","=","A");
		$search_result_type = null;
		$search_result_process = null;
		$search_result_startdate = null;
		$search_result_enddate = null;
		if(array_key_exists("page", $_REQUEST)){
			$page = $_REQUEST["page"];
		}
		if(array_key_exists("type", $_REQUEST) && !empty($_REQUEST["type"])){
			$feedback->and_where("type","=",$_REQUEST["type"]);
			$search_result_type = $_REQUEST["type"];
		}
		if(array_key_exists("process", $_REQUEST) && !empty($_REQUEST["process"])){
			$feedback->and_where("process","=",$_REQUEST["process"]);
			$search_result_process = $_REQUEST["process"];
		}
		if(array_key_exists("startdate", $_REQUEST) && !empty($_REQUEST["startdate"])){
			$feedback->and_where("createtime",">",$_REQUEST["startdate"]);
			$search_result_startdate = $_REQUEST["startdate"];
		}
		if(array_key_exists("enddate", $_REQUEST) && !empty($_REQUEST["enddate"])){
			$feedback->and_where("createtime","<",$_REQUEST["enddate"]);
			$search_result_enddate = $_REQUEST["enddate"];
		}
		$tmp = clone $feedback;
		$count = $tmp->count_all();

		$pagination = Pagination::factory(array(
	  		'total_items'    => $count,
		));

		$result_feedback = $feedback->order_by('createtime','DESC')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();

		$view->set_global('result_feedback', $result_feedback);
		
		$view->set_global('search_result_type', $search_result_type);
		$view->set_global('search_result_process', $search_result_process);
		$view->set_global('search_result_startdate', $search_result_startdate);
		$view->set_global('search_result_enddate', $search_result_enddate);

		$this->setTab($view);

		$page_links = $pagination->render();
		$view->set_global('page_links', $page_links);
		exit($view->render());
	}
	
	public function action_deleteAll(){
		if(ORM::factory("feedback")->deleteAll($_POST["ids"])){
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除成功";
			exit($view->render());
		}
	}

	private function baseInfo($view){
		$feedback = ORM::factory("feedback")->where("status","=","A");
		$count = $feedback->count_all();
		$pagination = Pagination::factory(array(
	  		'total_items'    => $count,
		));
		$result_feedback = $feedback->where("status","=","A")->order_by('createtime','DESC')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();

		$view->set_global('result_feedback', $result_feedback);

		$page_links = $pagination->render();
		$view->set_global('page_links', $page_links);
	}

}
